package cycles.util.text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class PlainString {

	public static String toString(Object[] objs) {
		StringBuilder sb = new StringBuilder('[');
		for (Object o : objs) {
			sb.append(o);
			sb.append(',');
		}
		sb.append(']');
		return sb.toString();
	}

	static final java.util.Random rand = new java.util.Random(
			System.currentTimeMillis());

	public static void main(String[] args) {
		int[] src = new int[] { 1, 2, 3, 4, 5 };
		System.out.println(concat(src, ','));

		List<int[]> vals = new ArrayList<int[]>();
		for (int i = 0; i < 5; i++) {
			if (i == 3)
				continue;
			vals.add(new int[] { rand.nextInt(100), rand.nextInt(100),
					rand.nextInt(100) });
		}
		System.out.println(concat(vals, ',', '='));

	}

	/**
	 * 切分二维字符串
	 * 
	 * @param src
	 * @param topsplit
	 * @param lowsplit
	 * @return
	 */
	public static String[][] split(String src, String topsplit, String lowsplit) {
		if (src == null || src.length() == 0) {
			return new String[0][0];
		}
		StringTokenizer token = new StringTokenizer(src, topsplit);
		String[][] ret = new String[token.countTokens()][];
		int st = 0;
		while (token.hasMoreTokens()) {
			ret[st++] = split(token.nextToken(), lowsplit);
		}
		return ret;
	}

	/**
	 * 切分二维数组，如果其中一组中有非整数，则此组为null
	 * 
	 * @param src
	 * @param topsplit
	 * @param lowsplit
	 * @return
	 */
	public static int[][] splitInt(String src, String topsplit, String lowsplit) {
		if (src == null || src.length() == 0) {
			return new int[0][0];
		}
		StringTokenizer token = new StringTokenizer(src, topsplit);
		int[][] ret = new int[token.countTokens()][];
		int st = 0;
		while (token.hasMoreTokens()) {
			ret[st++] = splitInt(token.nextToken(), lowsplit);
		}
		return ret;
	}

	public static final String concat(int[] src, char split) {
		if (src == null || src.length == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder(src.length * 4);
		for (int v : src) {
			sb.append(v);
			sb.append(split);
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * 合并数组的list
	 * 
	 * @param src
	 * @param listsplit
	 * @param arraysplit
	 * @return
	 */
	public static final String concat(List<int[]> src, char listsplit,
			char arraysplit) {
		if (src == null || src.size() == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int[] vals : src) {
			if (vals == null || vals.length == 0) {
				continue;
			}
			for (int val : vals) {
				sb.append(val);
				sb.append(arraysplit);
			}
			sb.setCharAt(sb.length() - 1, listsplit);
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * 切分字符串
	 * 
	 * @param src
	 * @param split
	 * @return
	 */
	public static final String[] split(String src, String split) {
		if (src == null || src.length() == 0) {
			return new String[0];
		}
		StringTokenizer token = new StringTokenizer(src, split);
		String[] ret = new String[token.countTokens()];
		int st = 0;
		while (token.hasMoreTokens()) {
			ret[st++] = token.nextToken();
		}
		return ret;
	}

	/**
	 * 切分为整形数组，如果有任意非整数，则返回null
	 * 
	 * @param src
	 * @param split
	 * @return
	 */
	public static final int[] splitInt(String src, String split) {
		if (src == null || src.length() == 0) {
			return new int[0];
		}
		StringTokenizer token = new StringTokenizer(src, split);
		int[] ret = new int[token.countTokens()];
		int st = 0;
		try {
			while (token.hasMoreTokens()) {
				ret[st++] = Integer.parseInt(token.nextToken());
			}
		} catch (NumberFormatException e) {
			return null;
		}
		return ret;
	}

	/**
	 * 解析将一批字符串参数
	 * 
	 * @param src
	 *            类似"prop1=value1;prop2=value2"格式的字符串
	 * @param split
	 *            一级分隔符
	 * @param subsplit
	 *            二级分隔符
	 * @return 以key,value方式存储的参数串
	 */
	public static final Map<String, String> splitParas(String src,
			String split, String subsplit) {
		String[] pairs = PlainString.split(src, split);
		Map<String, String> ret = new HashMap<String, String>();
		for (String s : pairs) {
			String[] pair = s.split(subsplit, 2);
			if (pair.length == 2) {
				ret.put(pair[0], pair[1]);
			}
		}
		return ret;
	}

	/**
	 * 拆分类似"prop1=value1;prop2=value2"格式的字符串
	 * 
	 * @param src
	 * @return
	 */
	public static final Map<String, String> splitParas(String src) {
		return splitParas(src, ";", "=");
	}
}
